/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.sales.hancadsa.client.view;

import br.com.sales.generics.client.components.autocomplete.widget.Autocomplete;
import br.com.sales.generics.client.components.mask.MaskEditHandler;
import br.com.sales.generics.client.components.scroll.HScroll;
import br.com.sales.hancadsa.client.presenter.HanCadSAPresenter;
import br.com.sales.hancadsa.client.presenter.HanCadSAUiHandler;
import static br.com.sales.hancadsa.client.presenter.consts.HanCadSAConts.CELL_EDITING;
import static br.com.sales.hancadsa.client.presenter.consts.HanCadSAConts.CELL_REMOVING;
import static br.com.sales.hancadsa.client.presenter.consts.HanCadSAConts.HEADER_ROW_INDEX;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.SourcesTableEvents;
import com.google.gwt.user.client.ui.TableListener;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;
import com.gwtplatform.mvp.client.ViewWithUiHandlers;

/**
 *
 * @author v
 */
public class HanCadSAView extends ViewWithUiHandlers<HanCadSAUiHandler> implements HanCadSAPresenter.Display {

    private static final HanCadSAViewUiBinder uiBinder = GWT.create(HanCadSAViewUiBinder.class);
    @UiField
    HTMLPanel container;
    @UiField
    TextBox dateEdit;
    @UiField
    TextBox userEdit;
    @UiField
    FlexTable itemsGrid;
    @UiField
    VerticalPanel productEdit;
    @UiField
    MaskEditHandler priceEdit;
    @UiField
    TextBox countEdit;
    @UiField
    Label totalSalesLbl;
    @UiField
    Label valueTotalSalesLbl;
    @UiField
    HScroll flextableScroll;
    @UiField
    VerticalPanel clientEdit;
    @UiField
    Button addItem;
    @UiField
    Button cancelItem;
    private Autocomplete completeClient;
    private Autocomplete completeProduct;

    @Inject
    public HanCadSAView() {
        container = uiBinder.createAndBindUi(this);
        flextableScroll.getElement().setId("myFlextableScroll");
        priceEdit.getElement().setId("decimal");
    }

    @Override
    public void build() {
        adjustStatusFields();
        createGrid();
        countEdit.setMaxLength(3);
        totalSalesLbl.setVisible(false);
        cancelItem.setVisible(false);
        valueTotalSalesLbl.setVisible(false);
        buildAutocomplete();
    }

    private void buildAutocomplete() {
        completeClient = Autocomplete.build()
                .setTableName("PESSOA_FISICA")
                .setAttributeNameDesc("NM_PESSOA_FISICA")
                .setAttributeNameID("NR_SEQUENCIA");
        clientEdit.add(completeClient);
        completeProduct = Autocomplete.build()
                .setTableName("PRODUTO")
                .setAttributeNameDesc("DS_PRODUTO")
                .setAttributeNameID("NR_SEQUENCIA");
        productEdit.add(completeProduct);
    }

    private void createGrid() {
        itemsGrid.insertRow(HEADER_ROW_INDEX);
        itemsGrid.getRowFormatter().addStyleName(HEADER_ROW_INDEX, "flextable-header");

        addColumn("Produto");
        addColumn("Preço(R$)");
        addColumn("Quantidade");
        addColumn("Editar");
        addColumn("Remover");

        itemsGrid.setCellSpacing(0);
        itemsGrid.addStyleName("flexttable");
        itemsGrid.getColumnFormatter().setWidth(0, "71%");
        itemsGrid.getColumnFormatter().setWidth(1, "100px");
        itemsGrid.addTableListener(new TableListener() {
            @Override
            public void onCellClicked(SourcesTableEvents sender,
                    int row, int cell) {
                if (row > 0) {
                    switch (cell) {
                        case CELL_EDITING: {
                            getUiHandlers().editRecorg(row);
                            break;
                        }
                        case CELL_REMOVING: {
                            itemsGrid.removeRow(row);
                            getUiHandlers().calculateSale();
                            break;
                        }
                    }
                }
            }
        });
    }

//    @UiHandler("priceEdit")
//    public void onPriceKeyPress(KeyPressEvent event) {
////        getUiHandlers().onPriceKeyPress(event);
//    }
    @UiHandler("addItem")
    public void onClickAddItem(ClickEvent event) {
        getUiHandlers().onClickAddItem(event);
    }

    @UiHandler("cancelItem")
    public void onClickRemove(ClickEvent event) {
        getUiHandlers().onClickCancelItem(event);
    }

    @UiHandler("finishedBtn")
    public void onClickfinishedSales(ClickEvent event) {
        getUiHandlers().onClickfinishedSales(event);
    }

    @UiHandler("cancelBtn")
    public void onClickCancel(ClickEvent event) {
        getUiHandlers().onClickCancel(event);
    }

    @UiHandler("newBtn")
    public void onClickNew(ClickEvent event) {
        getUiHandlers().onClickNew(event);
    }

    @Override
    public Widget createCellWidget(Object cellObject) {
        Widget widget;

        if (cellObject instanceof Widget) {
            widget = (Widget) cellObject;
        } else {
            widget = new Label(cellObject.toString());
        }
        return widget;
    }

    private void addColumn(Object columnHeading) {
        Widget widget = createCellWidget(columnHeading);
        int cell = itemsGrid.getCellCount(HEADER_ROW_INDEX);

        widget.addStyleName("flextable-column-label");

        itemsGrid.setWidget(HEADER_ROW_INDEX, cell, widget);

        itemsGrid.getCellFormatter().addStyleName(HEADER_ROW_INDEX, cell, "flextable-column-label-cell");
    }

    @Override
    public void setMandatory() {
        if ("".equals(completeProduct.getText())
                || completeProduct.getText() == null) {
            completeProduct.getElement().addClassName("mandatory");
        } else {
            completeProduct.getElement().removeClassName("mandatory");
        }
        if (priceEdit.getValue() == null || "".equals(priceEdit.getValue())) {
            priceEdit.getElement().addClassName("mandatory");
        } else {
            priceEdit.getElement().removeClassName("mandatory");
        }
        if (countEdit.getValue() == null || "".equals(countEdit.getValue())) {
            countEdit.getElement().addClassName("mandatory");
        } else {
            countEdit.getElement().removeClassName("mandatory");
        }
    }

    @Override
    public Label getValueTotalSalesLbl() {
        return valueTotalSalesLbl;
    }

    @Override
    public Label getTotalSalesLbl() {
        return totalSalesLbl;
    }

    @Override
    public void adjustStatusFields() {
        boolean f = false;
        userEdit.setEnabled(f);
        dateEdit.setEnabled(f);
    }

    @Override
    public Autocomplete getCompleteClient() {
        return completeClient;
    }

    @Override
    public FlexTable getItemsGrid() {
        return itemsGrid;
    }

    @Override
    public TextBox getDateEdit() {
        return dateEdit;
    }

    @Override
    public MaskEditHandler getPriceEdit() {
        return priceEdit;
    }

    @Override
    public Autocomplete getProductEdit() {
        return completeProduct;
    }

    @Override

    public TextBox getCountEdit() {
        return countEdit;
    }

    @Override
    public TextBox getUsuarioEdit() {
        return userEdit;
    }

    @Override
    public Widget asWidget() {
        return container;
    }

    @Override
    public Button getAddItem() {
        return addItem;
    }

    @Override
    public Button getCancelItem() {
        return cancelItem;
    }

    interface HanCadSAViewUiBinder extends UiBinder<HTMLPanel, HanCadSAView> {
    }
}
